package cn.frh.user.service.impl;

import cn.frh.basic.constant.BaseConstants;
import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.MD5Utils;
import cn.frh.basic.util.StrUtils;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.dto.UserDto;
import cn.frh.user.mapper.LogininfoMapper;
import cn.frh.user.mapper.UserMapper;
import cn.frh.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.security.util.Password;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    //基础的继承
    //扩展的自己写

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void register(UserDto userDto) {
        //一.校验
        //1.空校验
        if (StringUtils.isEmpty(userDto.getPhone())
                ||StringUtils.isEmpty(userDto.getPassword())
                ||StringUtils.isEmpty(userDto.getComfirmPassword())
                ||StringUtils.isEmpty(userDto.getVerifyCode())){
            throw new BusinessException("必要参数不能为空");
        }
        //2.判断密码是否一致
        if (!userDto.getPassword().equals(userDto.getComfirmPassword())){
            throw new BusinessException("必要参数不能为空");
        }
        //3.判断用户是否存在
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp != null){
            throw new BusinessException("该用户已注册");
        }
        // 4.判断验证码是否过期
        Object codeTemp = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTemp==null){
            throw new BusinessException("验证码已过期，请重新获取");
        }
        //5.判断验证码是否正确
        String code = codeTemp.toString().split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码错误");
        }
        //二.保存(Logininfo和User)
        //
        User user = userDto2User(userDto);
        Logininfo logininfo = userDto2Logininfo(user);
        logininfoMapper.save(logininfo);
        //设置logininfo的id
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }
    private Logininfo userDto2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//前台用户为1 后台0
        return logininfo;
    }


    private User userDto2User(UserDto userDto) {
        //准备返回对象
        User user = new User();
        //设置值
        user.setUsername(userDto.getPhone());
        user.setPassword(userDto.getPassword());
        user.setPhone(userDto.getPhone());
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        //通过盐值加密密码
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        //设置盐值
        user.setSalt(salt);
        //设置密码
        user.setPassword(md5Pwd);
        //返回对象
        return user;
    }




}
